<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="searchForm.realname" placeholder="用户名" clearable>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getUserList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button
          v-if="isAuth('sys/user/add')"
          type="primary"
          @click="addUser()"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
    >
      <el-table-column width="55" label="序号">
        <template slot-scope="scope">
          <span>{{ (page - 1) * limit + scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="realname" label="用户名"> </el-table-column>
      <el-table-column prop="login_name" label="登录名"> </el-table-column>
      <el-table-column prop="role_id" label="所属角色">
        <template slot-scope="scope">
          {{ scope.row.role_name }}
        </template>
      </el-table-column>
      <el-table-column
        prop="statu"
        label="状态"
        v-if="isAuth('sys/user/toggle')"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="scope.row.status === 1"
            @click="setToggle(scope.row)"
            >启用</el-button
          >
          <el-button
            type="text"
            v-if="scope.row.status === 0"
            @click="setToggle(scope.row)"
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="icon" width="260px" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys/user/reset_pass')"
            type="text"
            @click="resetPass(scope.row)"
            >重置密码</el-button
          >
          <el-button v-if="isAuth('sys/user/edit')" type="text" @click="editHandle(scope.row)">编辑</el-button>
          <el-button v-if="isAuth('sys/user/del')" type="text" @click="delHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="page"
      :page-size="limit"
      :total="total"
    >
    </el-pagination>

    <!--新增对话框-->
    <el-dialog
      :title="isAdd ? '新增用户' : '编辑信息'"
      :visible.sync="dialogVisible"
      width="600px"
      :before-close="handleClose"
    >
      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="用户名" prop="realname" label-width="100px">
          <el-input v-model="editForm.realname"></el-input>
        </el-form-item>

        <el-form-item
          v-if="isAdd"
          label="登录名"
          prop="login_name"
          label-width="100px"
        >
          <el-input v-model="editForm.login_name"></el-input>
          <el-alert
            title="初始密码为123456"
            :closable="false"
            type="info"
            style="line-height: 10px"
          ></el-alert>
        </el-form-item>

        <el-form-item label="所属角色" prop="role_id" label-width="100px">
          <el-select
            v-model="editForm.role_id"
            filterable
            placeholder="请选择角色"
          >
            <el-option
              v-for="item in roleList"
              :key="item.role_id"
              :label="item.role_name"
              :value="item.role_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('editForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Role",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,
      total: 0,
      page: 1,
      limit: 10,
      tableData: [],
      roleList: [],

      isAdd: false,
      dialogVisible: false,
      editForm: {},

      size: 10,
      current: 1,

      editFormRules: {
        realname: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
        login_name: [
          { required: true, message: "请输入登录名", trigger: "blur" },
        ],
        role_id: [
          { required: true, message: "请选择所属角色", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getUserList();
    this.getRoleList();
  },
  methods: {
    handleSizeChange(val) {
      this.limit = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getUserList();
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
      this.editForm = {};
    },
    //关闭表单
    handleClose() {
      this.resetForm("editForm");
    },
    //获取角色列表
    getRoleList() {
      this.$axios.post("admin/role/getAllRole").then((res) => {
        this.roleList = res.data.data.list;
      });
    },
    //获取用列表
    getUserList() {
      let data = {
        page: this.page,
        limit: this.limit,
        search: this.searchForm,
      };
      this.$axios.post("admin/user/user_list", data).then((res) => {
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      });
    },
    //新增，编辑用户
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          this.editForm.user_id ? (url = "edit") : (url = "add");
          this.$axios.post(`/admin/user/${url}`, this.editForm).then((res) => {
            this.$message({
              showClose: true,
              message: "操作成功",
              type: "success",
              onClose: () => {
                this.getUserList();
              },
            });
            this.dialogVisible = false;
          });
        } else {
          //this.$message.error('请填写完整信息')
          return false;
        }
      });
    },
    //新增弹框
    addUser() {
      this.isAdd = true;
      this.dialogVisible = true;
    },
    //编辑弹框
    editHandle(data) {
      this.editForm = JSON.parse(JSON.stringify(data));
      this.isAdd = false;
      this.dialogVisible = true;
    },
    //设置启用禁用
    setToggle(e) {
      let text = "";
      e.status == 1
        ? (text = `确认禁用【${e.realname}】?`)
        : (text = `确认启用【${e.realname}】?`);
      this.$confirm(text, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$axios
          .post("admin/user/toggle_status", { id: e.user_id })
          .then((res) => {
            this.getUserList();
            this.$message({
              type: "success",
              message: res.data.msg,
            });
          });
      });
    },

    //删除用户
    delHandle(e) {
      this.$confirm("确认删除【" + e.realname + "】?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$axios.post("admin/user/delete", { id: e.user_id }).then((res) => {
          this.$message({
            showClose: true,
            message: "删除成功",
            type: "success",
            onClose: () => {
              this.getUserList();
            },
          });
        });
      });
    },
    //重置密码
    resetPass(e) {
      this.$confirm(
        "将重置用户【" + e.realname + "】的密码, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$axios
          .post("admin/user/reset_pass", { id: e.user_id })
          .then((res) => {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "success",
              onClose: () => {},
            });
          });
      });
    },
  },
};
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}
</style>